require File.join(File.dirname(`node --print "require.resolve('expo/package.json')"`), "scripts/autolinking")
require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

platform :ios, '12.4'
install! 'cocoapods', :deterministic_uuids => false

production = ENV["PRODUCTION"] == "1"

target 'taroDemo' do
  use_expo_modules!
  post_integrate do |installer|
    begin
      expo_patch_react_imports!(installer)
    rescue => e
      Pod::UI.warn e
    end
  end
  config = use_native_modules!

  # Flags change depending on the env values.
  flags = get_default_flags()

  use_react_native!(
    :path => config[:reactNativePath],
    # to enable hermes on iOS, change `false` to `true` and then install pods
    :production => production,
    :hermes_enabled => flags[:hermes_enabled],
    :fabric_enabled => flags[:fabric_enabled],
    :flipper_configuration => FlipperConfiguration.enabled,
    # An absolute path to your application root.
    :app_path => "#{Pod::Config.instance.installation_root}/.."
  )

  target 'taroDemoTests' do
    inherit! :complete
    # Pods for testing
  end

  post_install do |installer|
    # !!! attention: for react native debug at release build only. remove next lines for your own application.
    find_and_replace("../node_modules/react-native/React/CoreModules/RCTDevLoadingView.mm","[self showOfflineMessage];","")
    find_and_replace("../node_modules/react-native/React/Base/RCTDefines.h","#define RCT_DEV 0", "#define RCT_DEV 1")
    find_and_replace("../node_modules/react-native/React/Base/RCTKeyCommands.m", "#if RCT_DEV", "#if DEBUG")
    # !!! end
    react_native_post_install(installer)
    __apply_Xcode_12_5_M1_post_install_workaround(installer)
    # Add these lines for Xcode 14 builds
    installer.generated_projects.each do |project|
      project.targets.each do |target|
          target.build_configurations.each do |config|
              config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
          end
      end
    end
    # End of added lines
  end
end

def find_and_replace(dir, findstr, replacestr)
  Dir[dir].each do |name|
      text = File.read(name)
      replace = text.gsub(findstr,replacestr)
      if text != replace
          puts "Fix: " + name
          File.open(name, "w") { |file| file.puts replace }
          STDOUT.flush
      end
  end
  Dir[dir + '*/'].each(&method(:find_and_replace))
end
